package com.lin.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.demo.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author zimingl
 * @Date 2022/9/25 18:00
 * @Description: TODO
 */
@Mapper     // 扫描mapper
@Repository // IDEA在 userMapper 处报错，因为找不到注入的对象，因为类是动态创建的，
            // 但是程序可以正确的执行。为了避免报错，可以在mapper接口上添加 @Repository 注解
public interface UserMapper extends BaseMapper<User> {

    @Select("select id,name,age,email from user")
    List<User> getUserList();

    @Delete("delete from user where id = #{id}")
    int deleteByIdInt(Long id);

    @Delete("delete from user where id = #{id}")
    boolean deleteByidBoolean(Long id);

    @Update(value = "update user set name = #{name}, age = #{age} where id = #{id}")
    int updateOneUser(User user);

    @Update("UPDATE user SET is_delete=1 WHERE id=#{id} AND is_delete=0")
    int logicDeleteById(Long id);

    @Select("select id,name,age,email from user where is_delete = 0")
    List<User> logicSelectAll();

    /**
     *
     * @return
     */
    List<User> selectAllByMapperSql();

    /**
     *
     * @return
     */
    int deleteByIdMapperSql(long id);

    /**
     *
     * @param user
     * @return
     */
    int updateMapperSql(User user);

    /**
     * selectBySQL1
     */
    List<User> selectBySQL1(@Param("name") String name, @Param("age1") Integer age1, @Param("age2") Integer age2);

    /**
    * 根据年龄查询用户列表，分页显示
    * @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位
    * @param age 年龄
    * @return
    */
    Page<User> selectPageVo(@Param("page") Page<User> page, @Param("age") Integer age);

    /**
     *
     * select
     * 	id
     * 	,name
     * 	,age
     * 	,email
     * 	,is_delete
     * 	,sex
     * 		from user
     * 		where is_delete != 0
     * 		and name like concat('%', #{name}, '%')
     * 		and email = {#email}
     */
    Page<User> selectUserByLikeNameEmail(@Param("page") Page<User> page, @Param("name")String name, @Param("email")String email);
}
